﻿using Elev.API.DouBan;
using Elev.DataFactory;
using Elev.Farmworker.Utility;
using Elev.MoviesCenter.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Elev.MoviesCenter.Utility
{
    public class Common
    {
        private static string[] ExtensionArray = { ".mkv", ".mp4", ".avi", ".rmvb", ".rm" };

        public static List<VideoInfoEntity> LoadVideoInfo(string filePath)
        {
            List<VideoInfoEntity> list = new List<Entity.VideoInfoEntity>();
            string[] files = Directory.GetFiles(filePath);
            foreach (string fileName in files)
            {
                FileInfo file = new FileInfo(fileName);
                if (ExtensionArray.Contains(file.Extension))
                {
                    VideoInfoEntity entity = new Entity.VideoInfoEntity()
                    {
                        FileName = file.Name
                    };
                    list.Add(entity);
                }
            }
            return list;
        }

        public static TB_Videos VideoConvert(Movies selectMovies)
        {
            TB_Videos video = new TB_Videos();
            video.Guid = Guid.NewGuid();
            video.Name = selectMovies.title;
            video.OriginalName = selectMovies.original_title;
            foreach (string lang in selectMovies.languages)
            {
                video.Language += lang + " ";
            }
            foreach (string genre in selectMovies.genres)
            {
                video.Type += genre + " ";
            }
            foreach (var director in selectMovies.directors)
            {
                video.Director += director.name + " ";
            }
            foreach (var casts in selectMovies.casts)
            {
                video.Casts += casts.name + " ";
            }
            video.ReleaseDate = selectMovies.pubdates == null ? "" : selectMovies.pubdates[0];
            video.Durations = selectMovies.durations == null ? "" : selectMovies.durations[0];
            string name = Guid.NewGuid().ToString() + ".jpg";
            if (Downloader.DownloadPicture(selectMovies.images.large, LocalCenter.ImageCache + "\\" + name, 30))
            {
                video.Image = name;
            }
            video.Summary = selectMovies.summary;
            return video;
        }
    }
}
